﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Forums
{
    public partial class UserAvatar : System.Web.UI.Page
    {
        string connection;
        SqlConnection sqlConnection;
        SqlCommand sqlUserCheck;

        protected void Page_Load(object sender, EventArgs e)
        {
            string user_id;
            connection = WebConfigurationManager.ConnectionStrings["CentForumsDB"].ConnectionString;
            sqlConnection = new SqlConnection(connection);

            if (Request.QueryString["user_id"] == null)
            {
                user_id = Session["User"].ToString();
            }
            else
            {
                user_id = Request.QueryString["user_id"];
            }
            
            //retrieve avatar for user
            string sqlCommand = "SELECT avatar FROM Registration WHERE user_accnt = " + user_id;

            sqlConnection.Open();
            sqlUserCheck = new SqlCommand(sqlCommand, sqlConnection);
            SqlDataReader dataReader = sqlUserCheck.ExecuteReader();

            //if the user has an avatar
            if (dataReader.Read())
            {
                //display avatar
                Response.ContentType = "image/GIF";
                Response.BinaryWrite((byte[])dataReader["avatar"]);

            }
            sqlConnection.Close();
        }
    }
}
